<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/course/video' }">课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体内容	 -->
    <el-card>
      <!-- 添加轮播图 -->
      <el-row>
        <el-col :span="1">
          <el-button type="primary" @click="addDialog">添加轮播图</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="bannerList" border style="width: 100%">
        <el-table-column type="index" width="100"> </el-table-column>
        <el-table-column label="轮播图" width="380" prop="img">
          <template slot-scope="scope">
            <img :src="scope.row.img" style="height: 160px" />
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="权重" >
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
        </el-table-column>

        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-tooltip
                class="item"
                effect="dark"
                content="编辑"
                placement="top"
            >
              <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="mini"
                  @click="editBannerlist(scope.row)"
              ></el-button>
            </el-tooltip>

            <el-tooltip
                class="item"
                effect="dark"
                content="删除"
                placement="top"
            >
              <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="removeBanner(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
          align="left"
          @size-change="SizeChange"
          @current-change="CurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 3, 4]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加轮播图dialog对话框 -->

    <el-dialog
        align="left"
        title="添加轮播图"
        :visible.sync="addBannerVisible"
        width="50%"
        @close="closeAddBannerDialog"
    >
      <!--主体部分 -->
      <el-form
          :model="addBannerForm"
          status-icon
          :rules="addBannerFormRul"
          ref="addBannerFormRel"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="轮播排序" prop="weight">
          <el-input-number
              v-model="addBannerForm.weight"
              controls-position="right"
              @change="handleChange" :min="1" :max="10">
          </el-input-number>
        </el-form-item>
        <el-form-item label="添加时间"  prop="create_time">
          <el-col :span="3">
            <el-date-picker
                v-model="addBannerForm.create_time"
                type="datetime"
                placeholder="选择日期"
                align="right"
                value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <!-- 轮播图封面-->
        <el-form-item label="轮播图封面" prop="img" style="width: 300px">
          <el-upload
              action="http://localhost:8089/api/v1/pub/video/uploadImg"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              v-model="addBannerForm.img"
              :limit=1
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addBannerVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBanner">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改轮播图diaolog对话框 -->

    <el-dialog align="left" title="修改轮播图" :visible.sync="editBannerVisible" width="50%" >
      <!--主体部分 -->
      <el-form
          :model="editBannerForm"
          status-icon
          ref="editBannerFormRel"
          label-width="100px"
          class="demo-ruleForm"
          @close="closeEditBannerDialog"
      >
        <el-form-item label="轮播排序" prop="weight">
          <el-input-number
              v-model="editBannerForm.weight"
              controls-position="right"
              @change="handleChange" :min="1" :max="10">
          </el-input-number>
        </el-form-item>
        <el-form-item label="修改时间"  prop="create_time">
          <el-col :span="3">
            <el-date-picker
                v-model="editBannerForm.create_time"
                type="datetime"
                placeholder="选择日期"
                align="right"
                value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <!-- 轮播图封面-->
        <el-form-item label="轮播图封面" prop="img" style="width: 300px">
          <el-upload
              action="http://localhost:8089/api/v1/pub/video/uploadImg"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="editSuccess"
              v-model="editBannerForm.img"
              :limit=1
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      		<el-button @click="editBannerVisible = false">取 消</el-button>
      		<el-button type="primary" @click="editBanner">确 定</el-button>
      	</span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //轮播图列表
      bannerList: [],
      img: [],
      queryInfo: {
        pagenum: 1,
        pagesize: 2,
      },
      total: 0,
      //添加轮播图dialog,控制显示隐藏
      addBannerVisible: false,
      //添加轮播图参数
      addBannerForm: {
        id: "",
        img: "",
        create_time: "",
        weight: "",
      },
      //添加轮播图规则
      addBannerFormRul: {
        img: {
          required: true,
          message: "请添加轮播图封面",
          trigger: "blur",
        },
        create_time: {
          required: true,
          message: "请输入时间",
          trigger: "blur",
        },
        weight: {
          required: true,
          message: "请输入轮播顺序",
          trigger: "blur",
        },
      },
      //修改轮播图参数
      editBannerForm: {
        id: "",
        img: "",
        create_time: "",
        weight: "",
      },
      //修改轮播图dialog,控制显示隐藏
      editBannerVisible: false,
      //轮播图
      dialogImageUrl: '',
      dialogVisible: false,
    };
  },
  mounted() {
    this.getVideoBannerList();
  },
  methods: {
    //排序
    handleChange(value) {
      console.log(value);
    },
    //轮播图上传
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;

    },
    handleSuccess(response){
      this.addBannerForm.img=response.data;
    },
    editSuccess(response){
      this.editBannerForm.img=response.data;
    },
    //请求轮播图列表
    getVideoBannerList() {
      this.$http
          .get("api/v1/pub/video/videoBanner", {
            params: this.queryInfo,
          })
          .then((res) => {
            console.log(res);
            if (res.status != 200) {
              return this.$message.error("获取轮播图失败");
            } else {
              this.$message.success("获取轮播图成功");
              this.bannerList = res.data.list;
              this.total = res.data.total;
              for (let i = 0; i < this.bannerList.length; i++) {
                this.img[i] = this.bannerList[i].img;
              }
            }
          });
    },
    //每页数据条数改变时触发
    SizeChange(newval) {
      this.queryInfo.pagesize = newval;
      this.getVideoBannerList();
    },
    //当前页面改变时触发
    CurrentChange(newval) {
      this.queryInfo.pagenum = newval;
      this.getVideoBannerList();
    },
    //关闭添加轮播图对话框，重置表单
    closeAddBannerDialog() {
      this.$refs.addBannerFormRel.resetFields();
    },
    //关闭添加轮播图对话框，重置表单
    closeEditBannerDialog() {
      this.$refs.editBannerFormRel.resetFields();
    },
    //点击添加轮播图
    addDialog() {
      this.addBannerVisible = !this.addBannerVisible;
    },
    //点击确定发送添加轮播图接口
    addBanner() {
      this.$refs.addBannerFormRel.validate((valid) => {
        //校验规则全部正确向后台发送请求
        if (!valid) return;
        this.$http.post("api/v1/pub/video/addBanner", this.addBannerForm).then((res) => {
          console.log(res)
          if(res.data.code!==0) return this.$message.error("添加轮播图失败");
          this.$message.success("添加轮播图成功");
          this.getVideoBannerList();
          this.addBannerVisible = !this.addBannerVisible;
        });
      });
    },
    //删除轮播图
    removeBanner(id) {
      this.$http.delete(`api/v1/pub/video/removeBanner/${id}`).then((res) => {
        console.log(res)
        if(res.data.code!==0) return this.$message.error("删除轮播图失败");
        this.$message.success("删除轮播图成功");
        //刷新列表
        this.getVideoBannerList();
      });
    },
    //修改轮播图事件
    editBannerlist(item){
      this.editBannerVisible = !this.editBannerVisible;
      this.editBannerForm={
        id: item.id,
        img: item.img,
        create_time: item.create_time,
        weight: item.weight
      }
    },
    //点击确定发送请求轮播图信息
    editBanner(){
      this.$http.put("api/v1/pub/video/editBanner",this.editBannerForm).then(res=>{
        //console.log(res);
        if(res.data.code!==0) return this.$message.error("修改轮播图失败！");
        this.$message.success("修改轮播图成功！");
        this.getVideoBannerList();
        this.editBannerVisible = !this.editBannerVisible;
      })
    },
  },
};
</script>

<style>
</style>